import java.util.*;

public class BinaryTree {
    public class TreeNode {
        public int val;
        public TreeNode right;
        public TreeNode left;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    private TreeNode root;

    public TreeNode creatree() {
        TreeNode node1 = new TreeNode(11);
        TreeNode node2 = new TreeNode(12);
        TreeNode node3 = new TreeNode(13);
        TreeNode node4 = new TreeNode(14);
        TreeNode node5 = new TreeNode(15);
        TreeNode node6 = new TreeNode(16);
        TreeNode node7 = new TreeNode(17);
        root = node1;
        node1.left = node2;
        node1.right = node3;
        node2.left = node4;
        node2.right = node5;
        node3.left = node6;
        node3.right = node7;
        return root;

    }

    //前序遍历
    public void preOrder(TreeNode root){
        if (root == null) {
            return;
        }
        System.out.println(root);
        preOrder(root.left);
        preOrder(root.right);
    }
    //非递归前序遍历
    public void preOrderN(TreeNode root){
        if(root == null){
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        stack.push(cur);
        while(cur!=null||!stack.isEmpty()) {
            while (cur != null) {
                System.out.print(cur.val);
                cur = cur.left;
                stack.push(cur);
            }
            cur = stack.pop();
            System.out.println(cur);

        }
    }


    //中序遍历
    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.println(root);
        inOrder(root.right);
    }
    //非递归中序遍历
    public void inOrderN(TreeNode root){
        if(root == null){
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur!=null||!stack.isEmpty()){
        while(cur!=null){
            stack.push(cur);
            cur = cur.left;

        }
            TreeNode top = stack.pop();
            System.out.print(top.val);
            cur = top.right;
        }
    }
    //后续遍历
    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.println(root);
    }
    //非递归后续遍历  左  右  根
    public void postderN(TreeNode root){
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode prev = null;
        while (cur!=null||!stack.isEmpty()){
            while (cur!=null){
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.peek();
            if(top.right==null||prev == top.right){
                System.out.print(top.val+" ");
                prev = top;
                stack.pop();
            }else {
                cur = top.right;
            }

        }
    }

    //获取树中结点的个数
    public static int size = 0;

    public int size1(TreeNode root) {

        if (root == null) {
            return 0;
        }
        size++;
        size1(root.left);
        size1(root.right);

        return size;
    }

    public int size2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return size2(root.left) + size2(root.right) + 1;
    }

    //获取叶子结点个数
    public static int LeafCount = 0;

    public void getLeafNodeCount1(TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {

            LeafCount++;
        }

        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
    }

    public int getLeafNodeCount2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {

            return 1;
        }

        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }

    //获取第K层结点的个数
    public int getKLeveNodeCount(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLeveNodeCount(root.right, k - 1) + getKLeveNodeCount(root.left, k - 1);

    }

    //二叉树的高度
    public int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int heightLeft = getHeight(root.left);
        int heightRight = getHeight(root.right);
        return Math.max(heightLeft, heightRight) + 1;
    }

    //检测值 为 value 的元素是否存在
    public TreeNode find(TreeNode root, int val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        TreeNode nodeLeft = find(root.left, val);
        if (nodeLeft != null) {
            return nodeLeft;
        }
        TreeNode noderight = find(root.right, val);
        if (noderight != null) {
            return noderight;
        }
        return null;
    }

    //层序遍历
    public void levelOrder(TreeNode root){
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        TreeNode cur = root;
        queue.offer(root);
        while(!queue.isEmpty()){
            cur = queue.poll();
            System.out.println(cur);
            if(cur.left!=null){
                queue.offer(cur.left);
            }if(cur.right!=null){
                queue.offer(cur.right);
            }
        }
    }

    public TreeNode invertTree(TreeNode root) {//反转二叉树
        if (root == null) {
            return null;
        }
        if (root.left == null && root.right == null) {
            return root;
        }
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }

    //判断两棵树是否相同
    public boolean isSameTree(TreeNode p, TreeNode q) {
        //判断两个树的结构是否一样
        if (p != null && q == null || p == null && q != null) {
            return false;
        }
        //如果第一个if 语句没有执行
        //要么都为空 要么都不为空
        if (p == null && q == null) {
            return true;
        }
        //不为空比较两个结点的值
        if (p.val != q.val) {
            return false;
        }
        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }

    //判断这棵树是不是对称的树
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        return isSymmetricN(root.left, root.right);
    }

    public boolean isSymmetricN(TreeNode leftTree, TreeNode rightTree) {
        if (leftTree == null && rightTree != null || leftTree != null && rightTree == null) {
            return false;
        }
        if (leftTree == null && rightTree == null) {
            return true;
        }
        if (leftTree.val != rightTree.val) {
            return false;
        }
        return isSymmetricN(leftTree.left, rightTree.right) && isSymmetricN(leftTree.right, rightTree.left);
    }


    //判断sub是不是root的一个子树
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if (root == null) {
            return false;
        }
        if (isSameTree(root, subRoot)) {
            return true;
        }

        return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
    }


    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        int leftH = getHeightN(root.left);
        int rightH = getHeightN(root.right);
        return Math.abs(leftH - rightH) < 2 && isBalanced(root.left) && isBalanced(root.right);
    }

    public int getHeightN(TreeNode root) {
        if (root == null) {
            return -1;
        }
        int leftH = getHeightN(root.left);
        int rightH = getHeightN(root.right);
        return Math.max(leftH, rightH) + 1;
    }

    //层序遍历
    public void levelOrderN1(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        TreeNode cur = root;
        queue.offer(root);
        while (!queue.isEmpty()) {
            cur = queue.poll();
            System.out.println(cur);
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }
//层序遍历
    public void levelOrderN(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode cur = root;
        queue.offer(cur);
        while (!queue.isEmpty()) {
            cur = queue.poll();
            System.out.print(cur + " ");
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }
    //自底向上的层序遍历
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> listN = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if (root == null) {
            return listN; // 处理空树，避免空指针异常
        }
        TreeNode cur = root;
        queue.offer(root);
        while (!queue.isEmpty()){
            int size_queue = queue.size();
            List<Integer> list = new ArrayList<>();

            while(size_queue!=0){
                cur = queue.poll();
                list.add(cur.val);

                if(cur.left!=null){
                    queue.offer(cur.left);
                }
                if(cur.right!=null){
                    queue.offer(cur.right);
                }
                size_queue--;
            }
            listN.add(0,list);
        }
        return listN;
    }

    //判断一棵树是不是完全二叉树
    public boolean isCompleteTree1(TreeNode root) {
        if (root == null) {
            return true;

        }
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = root;
            if (cur != null) {
                return false;
            } else {
                break;
            }
        }
        while (!queue.isEmpty()) {
            TreeNode peek = queue.peek();
            if (peek != null) {
                return false;
            }
            queue.poll();
        }
        return true;
    }

    public boolean isCompleteTree(TreeNode root) {
        if (root == null) {
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = root;
            cur = queue.poll();
            if (cur != null) {
                queue.offer(cur.left);
                queue.offer(cur.right);
            } else {
                break;
            }

        }
        while (!queue.isEmpty()) {
            TreeNode peek = queue.peek();
            if (peek != null) {
                return false;
            }
        }
        return true;
    }
    public List<List<Integer>> levelOrder1(TreeNode root) {
        List<List<Integer>> listN = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if (root == null) {
            return listN; // 处理空树，避免空指针异常
        }
        TreeNode cur = root;
        queue.offer(root);
        while (!queue.isEmpty()){
            int size_queue = queue.size();
            List<Integer> list = new ArrayList<>();

            while(size_queue!=0){
                cur = queue.poll();
                list.add(cur.val);

                if(cur.left!=null){
                    queue.offer(cur.left);
                }
                if(cur.right!=null){
                    queue.offer(cur.right);
                }
                size_queue--;
            }
            listN.add(list);
        }
        return listN;
    }


    //根据二叉树创建字符串
    public String tree2str(TreeNode root) {
        if(root == null){
            return null;
        }
        StringBuilder str1 = new StringBuilder();
        tree2strChil(root,str1);
        return str1.toString();
    }

    private StringBuilder tree2strChil(TreeNode root, StringBuilder str1) {
        if(root == null){
            return null;
        }
        str1.append(root.val);
        if(root.left!=null){
            str1.append('(');
            tree2strChil(root.left,str1);
            str1.append(')');
        }else {
            if(root.right!=null){
                str1.append("()");
            }else {
                return null;
            }

        }
        if(root.right!=null){
            str1.append('(');
            tree2strChil(root.right,str1);
            str1.append(')');
        }else {
            return null;
        }
        return str1;
    }

}



































